package com.tbit.main.controller;

import cn.hutool.core.util.StrUtil;
import com.tbit.main.pojo.AccountUser;
import com.tbit.main.pojo.Result;
import com.tbit.main.pojo.VO.QuickAccessVO;
import com.tbit.main.pojo.dto.AddQuickAccessDTO;
import com.tbit.main.pojo.dto.QueryQuickAccessDTO;
import com.tbit.main.pojo.dto.TodoItemsDTO;
import com.tbit.main.service.GeoreGionService;
import com.tbit.main.service.TokenService;
import com.tbit.main.service.WorkbenchService;
import com.tbit.manager.api.WorkbenchApi;
import com.tbit.manager.api.dto.ItemsDTO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 运维端工作台
 * @author <a href="mailto:liu.haoxiang@tbit.com.cn">BUGXiang</a>
 * @version 1.0
 * @since 2024-11-26
 */
@RestController
@RequestMapping("/workBench")
public class WorkbenchController {

    @Resource
    private WorkbenchService workbenchService;
    @Autowired
    private WorkbenchApi workbenchApi;
    @Resource
    private TokenService tokenService;
    @Resource
    private GeoreGionService georeGionService;


    /**
     * 查询常用功能
     * @param dto dto
     * @return result
     */
    @PostMapping("/queryQuickAccess")
    public Result<QuickAccessVO> queryQuickAccess(QueryQuickAccessDTO dto){
        return workbenchService.queryQuickAccess(dto);
    }


    /**
     * 添加常用功能
     * @param dto dto
     * @return result
     */
    @PostMapping("/addQuickAccess")
    public Result<Object> addQuickAccess(AddQuickAccessDTO dto){
    	return workbenchService.addQuickAccess(dto);
    }

    /**
     * 获取待办事项
     * @param dto dto
     * @return result
     */
    @PostMapping("/todoItems")
    public Result<Object> todoItems(TodoItemsDTO dto){
        ItemsDTO itemsDTO = new ItemsDTO();
        BeanUtils.copyProperties(dto, itemsDTO);
        itemsDTO.setType(0);
        Map<String, Object> map = workbenchApi.todoItems(itemsDTO);
        return Result.success(map);
    }


    /**
     * 获取今日数据
     * @param dto dto
     * @return result
     */
    @PostMapping("/getData")
    public Result<Object> getData(TodoItemsDTO dto, HttpServletRequest request){
        AccountUser accountUserToken = tokenService.getAccount(request.getHeader("token"));
        if (StrUtil.isEmpty(dto.getRegionId())) {
            dto.setRegionIds(georeGionService.getByAccountUserId(accountUserToken.getAccountUserId()));
        }else {
            String[] split = dto.getRegionId().split(",");
            List<Integer> regionIds = Arrays.stream(split)
                    .map(Integer::parseInt)
                    .collect(Collectors.toList());
            dto.setRegionIds(regionIds);
        }
        if (Objects.isNull(dto.getAccountId())){
            return Result.error("请选择区域");
        }
        return Result.success(workbenchService.getData(dto));
    }


}
